--
-- Author:      name
-- DateTime:    2018-04-27 10:59:15
-- Description: 节点间消息转发，服务间消息转发


local skynet = require "skynet"
local cluster = require("skynet.cluster")
local httpc = require("http.httpc")
local cjson = require("cjson")


---------------------------------------------------------
-- class
---------------------------------------------------------
local Message = class("Message")

function Message:ctor()
	self.web_host = "127.0.0.1:8001"
end


---------------------------------------------------------
-- 不同进程服务间发送消息
---------------------------------------------------------
--异步发送消息请求 node:节点名, service_name服务名, cmd消息名
function Message:sendNode(node, service_name, cmd, ... )
	-- print("############",type(cluster.send), node, service_name, cmd, ... )
	local ok, result = pcall(cluster.send, node, service_name, cmd, ...)
	if not ok then
		g.Log():error('##############Message.send faild:',result)
	end
	return result
end

--同步发送消息请求
function Message:callNode(node, service_name, cmd, ... )	
	-- print("________callNode ___",node, service_name, cmd, ...)
	local ok, res = pcall(cluster.call, node, service_name, cmd, ...)	
	if not ok then
		g.Log():error("############Message.call cmd:"..cmd.." faild:", res, node, service_name, cmd, ...)
		return false
	end
	return res
end

--异步发送到指定节点的.manager服务
function Message:sendNodeManager(node, cmd, ...)
	self:sendNode(node,'.manager', cmd, ...)
end

--同步发送到指定节点的.manager服务
function Message:callNodeManager(node, cmd, ...)
	local waiting_co = coroutine.running()
	local res = nil
	local function call_wrapper( ... )
		res = self:callNode(node, '.manager', cmd, ...)
		skynet.wakeup(waiting_co)
	end
    skynet.fork(call_wrapper,...)    
    --超时返回失败
    local timeout = timeout or 5
    if skynet.sleep(timeout * 100) == 'BREAK' then  
      	
        return true, res
    end
    g.Log():error("__callNodeManager__发送消息超时___",node, cmd)
	return false, "发送消息超时,"..cmd
end



---------------------------------------------------------
-- 同进程服务间发送消息
---------------------------------------------------------
--异步给服务发消息
function Message:sendService(service_name, cmd, ...)
	skynet.send(service_name,'lua',cmd,...)
end

--同步给服务发消息
function Message:callService(service_name, cmd, ...)
	-- print("____service_name,",service_name)
	local ok, res = pcall(skynet.call, service_name,'lua',cmd,...)
	if ok then 
		return res
	end
	return false
end

--异步发送到指定节点的.manager服务
function Message:sendManager(cmd, ...)
	self:sendService('.manager',cmd,...)
end

--同步发送到指定节点的.manager服务
function Message:callManager(cmd, ...)
	return self:callService('.manager',cmd,...)
end

-- ---------------------------------------------------------
-- -- cluster
-- ---------------------------------------------------------
--给集群点广播消息
function Message:clusterBrocast(...)
	local cmd = "cluster_broadcast"
	return self:sendService('.manager', cmd , ...)
end

--给指定类型节点广播

--给指定类型节点中的一个发消息

--给指定的节点发消息

-- ---------------------------------------------------------
-- -- http请求
-- ---------------------------------------------------------
-- function Message:httpGet(cmd, data)
-- 	local ok, body = httpc.get(self.web_host, cmd, data)
-- 	if not ok then 
-- 		g.Log():error("_________httpReq__error__", cmd, data)
-- 		return 
-- 	end
-- 	return body
-- end

-- function Message:httpPost(cmd, data)
-- 	local ok, body = httpc.post(self.web_host, cmd, data)
-- 	if not ok then 
-- 		g.Log():error("_________httpReq__error__", cmd, data)
-- 		return 
-- 	end
-- 	return body
-- end



return Message